#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File    : graph.py
@Time    : 2023/01/27 00:43:28
@Author  : 郭瑞强
@Contact : sunraing@126.com
@Version : 0.1
@License : BSD 3-Clause License
@Desc    : 节点的实现
"""
from __future__ import annotations
from .state import State


class Vertex:
    """构成图的节点存储结构"""

    def __init__(self, key) -> None:
        self.id = key
        self.connected: dict[Vertex, int] = dict()  # 不考虑边的权重情况可以用set或list代替

        self.distance: int = 0  # 宽度优先搜索（按层搜索）标识量,搜索的层数
        self.state: State = State.unreached
        self.pre_vertex: Vertex = None

        self.discover_t = 0  # 用于通用搜索
        self.explored_t = 0

    def __str__(self) -> str:
        return "Vertex {} connected to {}".format(
            self.id, str(x.id for x in self.connected)
        )

    def add_neighbor(self, nbr: Vertex, weight=1) -> None:
        self.connected[nbr] = weight

    def get_connections(self):
        return self.connected.keys()

    def get_id(self):
        return self.id

    def get_weight(self, nbr: Vertex):
        return self.connected.get(nbr, 0)

    def get_state(self):
        return self.state

    def set_state(self, s: State):
        self.state = s

    def get_pred(self):
        return self.pre_vertex

    def set_pred(self, vert: Vertex):
        self.pre_vertex = vert

    # 下面一般用于宽度搜索
    def get_distance(self):
        return self.distance

    def set_distance(self, d: int):
        self.distance = d

    # 下面一般用于通用搜索
    def get_discover(self):
        return self.discover_t

    def set_discover(self, dtime):
        self.discover_t = dtime

    def get_explored(self):
        return self.explored_t

    def set_explored(self, etime):
        self.explored_t = etime

    def __iter__(self):
        return iter(self.connected.keys())

    # 用于构建优先队列的比较运算
    def __lt__(self, vert: Vertex):
        return self.distance < vert.get_distance()
